<?php
header('Content-Type: text/html; charset=utf-8');
include('../includes/stemmer.php');
include('../includes/transliterate.php');
include('../class/class_connect.php');
include('../class/class_checkGetData.php');
if(!function_exists('mb_ucfirst')) {
    function mb_ucfirst($str, $enc = 'utf-8') {
    		return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
    }
}
$aCheck=new checkGetData('pr');
foreach($_GET as $key=>$vol)
{
	$_GET[$key]=$aCheck->safetyCheck($vol);
}
$aCheck->checkCity($_GET['city']);


$stopWords=array(
'и', 'И', 'или', 'Или', 'как', 'Как', 'Купить', 'купить', 'найти', 'куплю', 'а', 'А', 'у', 'У', 'в', 'В', 'на', 'На', 'с', 'С', 'По', 'по', '', ' ', '  ', '   '
);


//include('class/class_connect.php');
include('class/class_mobilepage.php');
include('class/abstract/class_paginator.php');
include('../class/city_rus.php');
include('../class/class_form.php');
include('../class/image_class.php');
session_start();
class searchPage extends page
{
	var $row=array();
	var $cityRus;
	var $searchRes=array();
	function __construct($searchRes=array())
	{
		$this->searchRes=$searchRes;
		$this->cityRus=new city_rus_class();
		parent::__construct();
	}
	function title()
	{
		echo $_GET['product'].' '.$this->cityRus->city_rus_im($_GET['city']);
	}
	function maincontent()
	{
		$stopWords=array(
		'и', 'И', 'или', 'Или', 'как', 'Как', 'Купить', 'купить', 'найти', 'куплю', 'а', 'А', 'у', 'У', 'в', 'В', 'на', 'На', 'с', 'С', 'По', 'по', '', ' ', '  ', '   '
		);
		echo ('
		<link rel="stylesheet" type="text/css" href="style/search.css" />
		<script type="text/javascript" src="scripts/productSuggest.js">
		</script>
		<div class="searchPage1">
			<div class="prodResults">
			');
		$aStemmer=new Lingua_Stem_Ru();
    	$aConnect=new connect('pr');
    	$rowShown=array();
    	$value=$_GET['product'];
    	for($Y=0; $Y<4; $Y++)
    	{
    		if($Y==1)$value=mb_strtolower($value, 'utf-8');
    		if($Y==0)$value=mb_ucfirst($value);
			if($Y==2)$value=mb_strtoupper($value, 'utf-8');
			if($Y==3)$value=mb_transliterate($value);
			$valueWords=explode(' ', $value);
			foreach($valueWords as $key=>$word)
			{
				if(in_array($word, $stopWords))continue;
				$stValueWord[]=$aStemmer->stem_word($word);
			}
			$stvalue=implode(' ', $stValueWord);
	    	$res=$aConnect->select_all($_GET['city'],
	    	"WHERE name='".$value."' or model='".$value."' or brend='".$value."'");
	    	while($row=mysql_fetch_array($res))
	    	{
	    		$rowShown[]=$row['id'];
	    		$img='';
	    		$firmLink='';
	    		$Headers = @get_headers('http://pro-gorod.com/pictures/pricepics/'.$_GET['city'].'/'.$row['firms_id'].'/'.$row['photo'].'.jpg');
				if(preg_match("|200|", $Headers[0]))
	    		{
	    			$img='<img src="http://pro-gorod.com/pictures/pricepics/'.$_GET['city'].'/'.$row['firms_id'].'/'.$row['photo'].'.jpg" height="130" />';
	    		}
	    		$Headers = @get_headers('http://pro-gorod.com/pictures/firmspics/'.$_GET['city'].'/'.$row['firms_id'].'.jpg');
				if(preg_match("|200|", $Headers[0]))
	    		{	    			$firmLink='<img src="http://pro-gorod.com/pictures/firmspics/'.$_GET['city'].'/'.$row['firms_id'].'.jpg" height="70" />';
	    		}
	    			else $firmLink=$row['firms_name'];
	    		$pr='
	    		<tr>
	    			<td class="prImage">
	    				'.$img.'
	    			</td>
	    			<td>
			    		<name>'.$row['name'].'</name> <brend>'.$row['brend'].'</brend> <model>'.$row['model'].'</model>
					</td>
					<td class="prFirm">
						<div class="price">
							'.$row['price'].'
						</div>
						<a href="firms.php?city='.$_GET['city'].'&id='.$row['firms_id'].'" title="'.$row['firms_name'].'">
							'.$firmLink.'<br />
						</a>
					</td>
				</tr>
	    		';
	    		//(!is_integer($row['price']))?$price=99999999:
	    		$price=$row['price'];
	    		if($price=='')$price='очень очень много';
	    		$prSearchRes[$pr]=$price;
	    	}
	    	$res=$aConnect->select_all($_GET['city'],
	    	"WHERE name LIKE '%".$value."%' or model LIKE '%".$value."%' or brend LIKE '%".$value."%'");
	    	while($row=mysql_fetch_array($res))
	    	{
	    		if(in_array($row['id'], $rowShown))continue;
	    		$img='';
	    		$firmLink='';
	    		$Headers = @get_headers('http://pro-gorod.com/pictures/pricepics/'.$_GET['city'].'/'.$row['firms_id'].'/'.$row['photo'].'.jpg');
				if(preg_match("|200|", $Headers[0]))
	    			$img='<img src="http://pro-gorod.com/pictures/pricepics/'.$_GET['city'].'/'.$row['firms_id'].'/'.$row['photo'].'.jpg" height="130" />';
	    		$Headers = @get_headers('http://pro-gorod.com/pictures/firmspics/'.$_GET['city'].'/'.$row['firms_id'].'.jpg');
				if(preg_match("|200|", $Headers[0]))
	    		{
	    			$firmLink='<img src="http://pro-gorod.com/pictures/firmspics/'.$_GET['city'].'/'.$row['firms_id'].'.jpg" height="70" />';
	    		}
	    		$pr='<tr>
	    			<td class="prImage">
	    				'.$img.'
	    			</td>
	    			<td>
			    		<name>'.$row['name'].'</name> <brend>'.$row['brend'].'</brend> <model>'.$row['model'].'</model>
					</td>
					<td class="prFirm">
						<div class="price">
									'.$row['price'].'
								</div>
						<a href="firms.php?city='.$_GET['city'].'&id='.$row['firms_id'].'" title="'.$row['firms_name'].'">
							'.$firmLink.'<br />
						</a>
					</td>
				</tr>
	    		';
	    		//(!is_integer($row['price']))?$price=99999999:
	    		$price=$row['price'];
	    		if($price=='')$price='очень очень много';
	    		$prSearchRes[$pr]=$price;
	    	}
	  	}
	  	$value=$_GET['product'];
	  	for($Y=0; $Y<4; $Y++)
    	{
    		$stValueWord=array();
    		if($Y==1)$value=mb_strtolower($value, 'utf-8');
    		if($Y==0)$value=mb_ucfirst($value);
			if($Y==2)$value=mb_strtoupper($value, 'utf-8');
			if($Y==3)$value=mb_transliterate($value);
			$valueWords=explode(' ', $value);
			foreach($valueWords as $key=>$word)
			{
				if(in_array($word, $stopWords))continue;
				$stValueWord[]=$aStemmer->stem_word($word);
			}
			$stvalue=implode(' ', $stValueWord);
	    	if($stValueWord)
	    	{
	    		$common=array();
	    		foreach($stValueWord as $key=>$stvalue)
	    		{
			    	$res=$aConnect->select_all($_GET['city'],
			    	"WHERE name LIKE '%".$stvalue."%' or model LIKE '%".$stvalue."%' or brend LIKE '%".$stvalue."%'");
			    	while($row=mysql_fetch_array($res))
			    	{			    		$img='';
			    		$firmLLink='';
			    		if(in_array($row['id'], $rowShown))continue;
			    		$Headers = @get_headers('http://pro-gorod.com/pictures/pricepics/'.$_GET['city'].'/'.$row['firms_id'].'/'.$row['photo'].'.jpg');
						if(preg_match("|200|", $Headers[0]))
			    			$img='<img src="http://pro-gorod.com/pictures/pricepics/'.$_GET['city'].'/'.$row['firms_id'].'/'.$row['photo'].'.jpg" height="130" />';
			    		$Headers = @get_headers('http://pro-gorod.com/pictures/firmspics/'.$_GET['city'].'/'.$row['firms_id'].'.jpg');
						if(preg_match("|200|", $Headers[0]))
			    		{
			    			$firmLink='<img src="http://pro-gorod.com/pictures/firmspics/'.$_GET['city'].'/'.$row['firms_id'].'.jpg" height="70" />';
			    		}
			    		$pr='<tr>
			    			<td class="prImage">
			    				'.$img.'
			    			</td>
			    			<td>
					    		<name>'.$row['name'].'</name> <brend>'.$row['brend'].'</brend> <model>'.$row['model'].'</model>
							</td>
							<td class="prFirm">
								<div class="price">
									'.$row['price'].'
								</div>
								<a href="firms.php?city='.$_GET['city'].'&id='.$row['firms_id'].'" title="'.$row['firms_name'].'">
									'.$firmLink.'<br />
								</a>
							</td>
						</tr>
			    		';
			    		//(!is_integer($row['price']))?$price=99999999:
			    		$price=$row['price'];
			    		if($price=='')$price='очень очень много';
			    		$common[$pr][$key]=$price;
			    	}
		    	}
		    	//echo count($common[$pr]);
		    	foreach($common as $prod=>$key)
		    	{
		    		if(count($key)==count($stValueWord))
		    			$prSearchRes[$prod]=$common[$prod][0];
		 		}
	    	}
    	}
    	//$prSearchRes=array_flip($prSearchRes);
    	//$prSearchRes=array_unique($prSearchRes);
    	//$prSearchRes=array_flip($prSearchRes);
        $a=new prSearchPaginator($prSearchRes);
    	//$prSearchRe=array_unique($prSearchRes);
	    echo ('
        	</div>
        	<div class="prSearchCenter">
				<center>
					<h3>Поиск в товарах и услугах</h3>
						<form name="prSearchForm" id="prSearchForm" action="products.php?city='.$_GET['city'].'" method="get">
							<input name="product" id="prSearchInput" type="text" value="'.$_GET['product'].'" placeholder="Поиск" autocomplete="off">
	                        <input name="city" type="hidden" value="'.$_GET['city'].'">
							<input type="submit" id="submitButton" value="Поиск">
						</form>
						<div class="prSearchRes">
						</div>
				</center>
			</div>
    	</div>
    	');
	}
}
class prSearchPaginator extends paginator
{
	var $products=array();
	function __construct($products=array())
	{
		$this->products=$products;
		parent::__construct(30);
	}
	function countRows()
	{
		return count($this->products);
	}
	function pageContent()
	{
		echo ('
		<table class="prTable" border="0">
	    	');
	    	if($this->products)
	    	{
	    		asort($this->products);
	    		///next($this->products);
	    		$i=-1;
		    	foreach($this->products as $product=>$price)
		    	{
		    		$i++;
		    		if($i<$this->lim)continue;
		    		if($i==($this->lim+$this->limit))break;
		    		echo $product;
		    		//echo $product;
		   		}
			}
			else
				echo '<center><h3>Поиск по запросу "'.$_GET['product'].'" в товарах и услугах результатов не дал</h3></center>';
	   		echo ('
    	</table>
    	');

	}
}

$anAddFirms=new searchPage($result);


?>
